#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import shutil
import codecs
import sys
import time
import setup

def updateGameConfig(cfgFile, userCfgFile, dataMap):
	fileRead = codecs.open(cfgFile, 'r' , 'utf-8')
	lines = fileRead.readlines()
	fileRead.close()

	fileWrite = codecs.open(userCfgFile, 'w' , 'utf-8')
	for line in lines:
		bKeyExist = False

		for key in dataMap:
			if line.startswith(key):
				bKeyExist = True
				break

		if True == bKeyExist:
			fileWrite.write(key + " = " + dataMap[key] + "\r\n")
		else:
			fileWrite.write(line)

	fileWrite.close()
	print("修改" + cfgFile + "成功！")

if __name__=="__main__":
	if( len(sys.argv) != 3 ):
		print("please input param( versionCode and versionName)")
		exit()

	#生成配置文件
	setup.main("")
	time.sleep(1)

	#修改verCode、verName
	configfile = "./userconfig/gameinfo.conf"
	versionCode = sys.argv[1]
	versionName = sys.argv[2]

	datamap = {}
	datamap["versionCode"] = versionCode
	datamap["versionName"] = versionName

	updateGameConfig(configfile,configfile,datamap)
